﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KBVault.Web.Infrastructure;
using KBVault.DAL.Entities;
using KBVault.DAL.Repository;

namespace KBVault.Web.Public
{
    public partial class ShowArticle : KBVaultPublicPage
    {
        protected new void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender,e);
            if (ActiveArticleId < 1)
            {
                Response.Redirect("~/Default.aspx");
                return;
            }
            Article a = ArticleRepository.Get(ActiveArticleId);
            if (!Page.IsPostBack)
            {                
                
                a.ViewCount += 1;
                ArticleRepository.Update(a);
                disqusContainer.DisqusTitle = articleTitle.Text = a.Title;
                disqusContainer.DisqusIdentifier = "KB-"+a.Id.ToString();                

                articleContent.InnerHtml = Server.HtmlDecode(a.Content);
                linkCategory.Text = a.Category.Name;
                linkCategory.NavigateUrl = String.Format("/Public/ShowCategory.aspx?Category={0}", a.CategoryId);
                linkCategory.ToolTip = String.Format("Go to category:{0}", a.Category.Name);
                linkEdit.Visible = IsAuthorized;
                linkEdit.NavigateUrl = String.Format("/Application/ArticleOps.aspx?Op=edit&Article={0}&{1}", a.Id,ReturnUrlParameter);
                linkExport.NavigateUrl = String.Format("/Public/Print.aspx?Article={0}",ActiveArticleId);
                articleTags.DataSource = a.Tags;
                articleTags.DataBind();
                rptAttachments.DataSource = a.Attachments;
                rptAttachments.DataBind();
                List<Article> similarArticles = ArticleRepository.GetSimilarArticles(a.Id);
                rptSimilarArticles.DataSource = similarArticles;
                rptSimilarArticles.DataBind();
                NoArticlesSpan.Visible = similarArticles.Count == 0;
                SetPageTitle(a.Title);
                SetPageDescription(a.Title);
                SetPageTags(String.Join(",", a.Tags.Select( t => t.Name).ToArray<string>()));
                                
            }
            ConfigureLikeButton();
        }

        private void ConfigureLikeButton()
        {
            // If user liked this before do not show like button
            Article a = ArticleRepository.Get(ActiveArticleId);
            if (Request.Cookies["Article" + ActiveArticleId.ToString()] != null)
            {
                btnLike.Visible = false;
                htmlLikeCount.Visible = true;                
                htmlLikeCount.InnerText = a.LikeCount.ToString() + " people liked this.";
            }
        }

        protected void btnLike_Click(object sender, EventArgs e)
        {
            if (ActiveArticleId < 1)
            {            
                return;
            }
            Article a = ArticleRepository.Get(ActiveArticleId);
            a.LikeCount++;
            ArticleRepository.Update(a);
            Response.Cookies["Article"+ActiveArticleId.ToString()].Value = "yes";
            Response.Cookies["Article" + ActiveArticleId.ToString()].Expires = DateTime.Now.AddYears(1);
            ConfigureLikeButton();
        }
        
    }
}